package com.find.answer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.find.answer.domain.dto.PageDto;
import com.find.answer.domain.dto.SetmealDto;
import com.find.answer.domain.http.Result;
import com.find.answer.domain.po.Category;
import com.find.answer.domain.po.Dish;
import com.find.answer.domain.po.Setmeal;
import com.find.answer.domain.po.SetmealDish;
import com.find.answer.service.CategoryService;
import com.find.answer.service.SetmealDishService;
import com.find.answer.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Seon
 * @date 2022/10/29 16:29
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @GetMapping("/page")
    public Result<?> getSetmealPage(PageDto pageDto,
                                    @RequestParam(value = "name", required = false) String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.getSetmealDtoPage(pageDto, name);
        return Result.success(setmealDtoPage);
    }

    @GetMapping("/{id}")
    public Result<?> getSetmealById(@PathVariable("id") Long id) {
        SetmealDto setmealDto = setmealService.getSetmealWithDish(id);
        return Result.success(setmealDto);
    }


    @CacheEvict(value = "setmealCache", allEntries = true)
    @PostMapping("")
    public Result<?> addSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.addSetmealWithDish(setmealDto);
        return Result.success("添加成功");
    }

    @CacheEvict(value = "setmealCache", allEntries = true)
    @DeleteMapping("")
    public Result<?> deleteSetmeal(@RequestParam("ids") List<Long> ids) {
        setmealService.deleteSetmealWithDish(ids);
        return Result.success("删除成功");
    }

    @PutMapping("")
    public Result<?> updateSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.updateSetmealWithDish(setmealDto);
        return Result.success("修改成功");
    }

    @PostMapping("/status/{status}")
    public Result<?> updateStatusById(@PathVariable("status") Integer status,
                                      @RequestParam("ids") List<Long> ids) {
        Setmeal setmeal = new Setmeal();
        ids.forEach(id -> {
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        });
        return Result.success("修改成功");
    }

    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId")
    @GetMapping("/list")
    public Result<?> getSetmealListById(Setmeal setmeal) {
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        List<Setmeal> setmeals = setmealService.list(wrapper);
        return Result.success(setmeals);
    }
}
